﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using CID.Domain.Infrastructure;
using CID.Domain.Operations;
using CID.Web.Models.Infrastructure;

namespace CID.Web.Models.Operations
{
    public class LocationViewModel : BaseDescribedViewModel<ILocation>
    {
        #region Properties
        [Display(Name = "Deployment Agent")]
        public string DeploymentAgent { get; set; }

        [Required(ErrorMessage = "You must provide a Deployment Agent")]
        public int? DeploymentAgentId { get; set; }
        #endregion

        public LocationViewModel() : base() { }
        
        public LocationViewModel(ILocation model) : base(model)
        {
            if (model.DeploymentAgent != null)
            {
                DeploymentAgent = model.DeploymentAgent.Name;
                DeploymentAgentId = model.DeploymentAgent.Id;
            }
        }

        public override void UpdateModel(ILocation model)
        {
            base.UpdateModel(model);
            model.DeploymentAgent = deploymentAgents.Single(x => x.Id == DeploymentAgentId.Value);
        }

        #region Edition Helper Members
        private IEnumerable<DeploymentAgent> deploymentAgents;

        public void SetDeploymentAgents(IEnumerable<DeploymentAgent> deploymentAgents)
        {
            this.deploymentAgents = deploymentAgents;
        }

        public IEnumerable<DeploymentAgentViewModel> AvailableDeploymentAgents
        {
            get { return deploymentAgents.ToList().Select(x => new DeploymentAgentViewModel(x)); }
        }
        #endregion
    }
}